package sw.EngAdroid;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

//The game class contains the running methods to actually run the game
//It creates a PlayerClass Obj to store player information and keep
//track of players and their stats

public class NetworkGame extends Game{

	String name;
	ArrayList<Player> players = new ArrayList<Player>(); // Array list of the
															// players in the
															// game
	// ArrayList<Question> questions = new ArrayList<Question>(); // ArrayList
	// of questions for the game
	int num_players; // number of players in the game, for multiplayer set up?
	String score_type;
	/*
	 * how are we going to do scoring? how are we going to end the game? 1) 7
	 * turns, max score 2) score limit
	 */
	Question current_question; // Current question
	int nextQuestion = 0; // Location in the question ArrayList
	ArrayList<Guess> guesses = new ArrayList<Guess>(); // ArrayList of the
														// guesses for the
														// current question
	private ArrayList<Bet> bets = new ArrayList<Bet>(); // ArrayList of the bets for the
												// current question
	private ArrayList<Question> questions = new ArrayList<Question>(); // ArrayList
	private boolean running = false; // Checks whether the game is running or
    								 // not.
	
	
	/*
	 * Default constructor sets the game's name to "game" and the number of
	 * players to 4
	 */
	public NetworkGame() {
		name = "game";
		num_players = 4;
	}

	/*
	 * Constructor
	 */
	public NetworkGame(String game_name, int players) {
		name = game_name;
		num_players = players;
//		questions = getQuestionsFromFile("..//Questions.txt");
		for (int i = 0; i < 10; i++) {
			questions.add(new Question());
		}
		current_question = questions.get(nextQuestion);
	}

	/*
	 * Getter for name variable
	 */
	public String getName() {
		return name;
	}

	/*
	 * Setter for name variable
	 */
	public void setName(String n) {
		name = n;
	}

	/*
	 * Adds players to the game after checking if the game is full
	 */
	public boolean addPlayer(Player p) {
		if (num_players > players.size()) {
			players.add(p);
			return true;
		} else
			return false;
	}

	/*
	 * Updates the current question and displays it Currently does a print line
	 */
	public void displayQuestion() {
		current_question = questions.get(nextQuestion);
		System.out.println(current_question.getText());
	}

	/*
	 * Displays the answer for the current question, currently as a print line
	 * Increments nextQuestion and updates current_question
	 */
	public void displayAnswer() {
		System.out.println(current_question.getAnswer());
		nextQuestion++;
		current_question = questions.get(nextQuestion);
	}

	/*
	 * Used to add a guess to the guesses ArrayList
	 */
	public void receiveGuess(Guess guess) {
		guesses.add(guess);
	}

	public void clearGuesses() {
		guesses = new ArrayList<Guess>();
	}

	public ArrayList<Bet> getBetList() {
		return bets;
	}

	public void clearBets() {
		bets = new ArrayList<Bet>();
	}

	/*
	 * Used to add a bet to the bets ArrayList
	 */
	
	//TODO:
	//Send bet to server
	
	public void receiveBet(Bet bet) {
		bets.add(bet);
	}

	/*
	 * Prints out the bets received Useful for testing/debugging.
	 */
	public void getBets() {
		for (int i = 0; i < bets.size(); i++) {
			System.out.println(bets.get(i).getName() + "  "
					+ bets.get(i).getChoice() + "   "
					+ bets.get(i).getPermaChips() + "  "
					+ bets.get(i).getGameChips());
		}
	}

	/*
	 * Calculates results from bets To be used after all bets are in
	 */
	public void winnings() {
		//TODO:
		//Insert code to pull answer and guesses from server
		
		int answer = current_question.getAnswer();
		Guess winner = guesses.get(0);
		int win_location = 0;
		// The for loop below determines which guess was closest to the answer
		for (int i = 0; i < guesses.size(); i++) {
			int difference = Math.abs(answer - guesses.get(i).getGuess());
			if (difference < (Math.abs(answer - winner.getGuess()))) {
				winner = guesses.get(i);
				win_location = i;
			}
		}
		/*
		 * The for loop below gives chips to whoever bet on the winning guess
		 * Currently it just gives them a 2x return on their bet Doesn't
		 * currently remove chips for bets on wrong guesses
		 */
		
		//TODO:
		//Modify code to update plaer's chips on server, update all players' chips locally
		
		for (int j = 0; j < bets.size(); j++) {// walk through each bet
			if (bets.get(j).getChoice() == win_location) {
				String name = bets.get(j).getName();
				for (int z = 0; z < players.size(); z++) {
					if (name.equals(players.get(z).getPlayer_name())) {
						players.get(z).setGame_chips(
								bets.get(j).getPermaChips() + bets.get(j).getGameChips()
										+ players.get(z).getGame_chips());
					}
				}
			} else {
				String name = bets.get(j).getName();
				for (int z = 0; z < players.size(); z++) {
					if (name.equals(players.get(z).getPlayer_name())) {
						players.get(z).setGame_chips(
								players.get(z).getGame_chips()
										- bets.get(j).getGameChips());
						if (players.get(z).getGame_chips() < 0)
							players.get(z).setGame_chips(0);
					}
				}
			}
		}
	}

	/*
	 * Prints out the guesses Useful for testing/debugging.
	 */
	public List<Guess> getGuesses() {
		return guesses;
	}

	public ArrayList<Guess> get_Guesses() {
		return guesses;
	}

	public void resetChipBets() {
		for (int i = 0; i < players.size(); i++) {
			players.get(i).setChipsBet(0);
		}
	}

	public Question getQuestion() {
		//TODO:
		//Insert code to pull current_question from the server
		current_question = questions.get(nextQuestion);
		return current_question;
	}

	public List getQuestions() {
		return questions;
	}

	public ArrayList getPlayers() {
		//TODO:
		//Insert code to pull players from the server, update global variable
		return players;
	}

	 /*
     * returns running boolean
     */
    public boolean getRunning() {
            return running;
    }

    /*
     * sets the running boolean
     */
    public void setRunning(boolean selection) {
            running = selection;
    }
	
	private ArrayList<Question> getQuestionsFromFile(String fileName) {
		ArrayList<Question> questions = new ArrayList<Question>();
		try {
			Scanner in = new Scanner(new File(fileName));
			while (in.hasNextLine()) {
				String question = in.nextLine();
				int answer = in.nextInt();
				in.nextLine();
				questions.add(new Question(question, answer));
			}
		} catch (Exception e) {
			System.out.println(e);
		}
		Collections.shuffle(questions);
		return questions;
	}

}